package algorithm.stack;

import java.util.Stack;

/**
 *设计一个有getMin功能的栈
 * Created by zhy on 2017/9/2.
 */
public class Main2 {
    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;

    public Main2() {
        this.stackData = new Stack<Integer>();
        this.stackMin = new Stack<Integer>();
    }
    public void push(int newNum){
        if(this.stackMin.isEmpty()){
            this.stackMin.push(newNum);
        }else if(newNum < this.getMin()){
            this.stackMin.push(newNum);
        }else{
            int newMin = this.stackMin.peek();
            this.stackMin.push(newMin);
        }
    }

    public Integer pop(){
        if(this.stackData.isEmpty()){
            throw new RuntimeException("Your stack is empty");
        }
        stackMin.pop();//不需要为空判断，因为两个栈中的元素个数相同
        return stackData.pop();
    }

    public Integer getMin(){
        if(this.stackMin.isEmpty()){
            throw new RuntimeException("Your stack is empty");
        }
        return stackMin.peek();
    }

    public static void main(String[] args) {
        Main2 main2 = new Main2();
        main2.push(2);
        main2.push(5);
        main2.push(6);
        main2.push(1);
        main2.push(0);
        main2.push(7);
        System.out.println(main2.getMin());
    }
}
